package com.jzg.jcptadmin.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jzg.jcptadmin.R;

/**
 * Created by jzg on 2015/12/22.
 */
public class ProgressView extends RelativeLayout {
    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private PercentageRing ring;

    public ProgressView(Context context) {
        super(context);
        initView(context);
    }


    public ProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public ProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.progressview, null);
        tv1 = (TextView) view.findViewById(R.id.weiwancheng_tv);
        tv2 = (TextView) view.findViewById(R.id.yiwancheng_tv);
        tv3 = (TextView) view.findViewById(R.id.title);
        ring = (PercentageRing) view.findViewById(R.id.Circle);
        this.addView(view);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        this.setGravity(Gravity.CENTER);
        this.setLayoutParams(params);
    }

    /**
     * 设置进度条的数据
     * @param yiwancheng 已完成数
     * @param zongshu    总数
     * @param title      标题
     * @param color      进度条颜色
     */
    public void setData(int yiwancheng, int zongshu, String title, int color) {
        tv1.setText(yiwancheng + "");
        tv2.setText("/" + zongshu);
        tv1.setTextColor(color);
        tv3.setText(title);
        if(zongshu==0){
            ring.setTargetPercent(0);
        }else{
            ring.setTargetPercent(yiwancheng * 100 / zongshu);
        }
        ring.setTargetColor(color);
    }

    /**
     *
     * 更新进度条数据
     * @param yiwancheng 已完成数
     * @param zongshu    总数
     */
    public void update(int yiwancheng, int zongshu) {
        tv1.setText(yiwancheng + "");
        if(zongshu==0){
            ring.setTargetPercent(0);
        }else{
            ring.setTargetPercent(yiwancheng * 100 / zongshu);
        }
        tv2.setText("/" + zongshu);
        ring.update();
    }
}
